package cn.hd01.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import cn.hd01.common.BaseConstant;
import cn.hd01.repository.BookRecordRepository;
import cn.hd01.repository.entity.BookRecord;
import cn.hd01.service.BookRecordService;

@Service
public class BookRecordServiceImpl extends BaseServiceImpl<BookRecord, Integer> implements BookRecordService {

	private BookRecordRepository repository;
	
	@Autowired
	public BookRecordServiceImpl(BookRecordRepository repository) {
		super(repository);
		this.repository = repository;
	}

	@Override
	public Page<BookRecord> findAll(final BookRecord bookRecord, Pageable pageable) {
		if (bookRecord == null) {
			return repository.findAll(pageable);
		}

		return repository.findAll(Specifications.where(new Specification<BookRecord>() {
			@Override
			public Predicate toPredicate(Root<BookRecord> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<Predicate>();
				
				if (bookRecord.getCustomerId() != null) {
					predicates.add(cb.equal(r.get("customerId").as(Integer.class), bookRecord.getCustomerId()));
				}
				
				if (StringUtils.hasText(bookRecord.getCustomerName())) {
					predicates.add(cb.like(r.get("customerName").as(String.class), "%" + bookRecord.getCustomerName() + "%"));
				}
				
				if(!CollectionUtils.isEmpty(bookRecord.getDistributorIdList())){
					predicates.add(r.get("distributorId").in(bookRecord.getDistributorIdList()));
				}
				
				return cb.and(predicates.toArray(new Predicate[] {}));
			}
		}), pageable);
	}

	@Override
	public int updateBookRecordStatus(Integer id, String status) {
		return repository.updateBookRecordStatus(status, id);
	}

	@Override
	public List<BookRecord> findSmsRecord(final String beginTime,final String endTime) {
		if(StringUtils.isEmpty(beginTime) && StringUtils.isEmpty(endTime)){
			return repository.findAll();
		}
		
		return repository.findAll(Specifications.where(new Specification<BookRecord>() {
			@Override
			public Predicate toPredicate(Root<BookRecord> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<Predicate>();
				
				predicates.add(cb.equal(r.get("status").as(String.class), BaseConstant.Status.INVALID));
				if(StringUtils.hasText(beginTime)){
					predicates.add(cb.greaterThan(r.get("bookDate").as(String.class), beginTime));
				}
				
				if(StringUtils.hasText(endTime)){
					predicates.add(cb.lessThan(r.get("bookDate").as(String.class), endTime));
				}
				
				return cb.and(predicates.toArray(new Predicate[] {}));
			}
		}));
	}

	@Override
	public Integer countRecord(Integer customerId) {
		return repository.countRecord(customerId);
	}
}
